 <?php

class MaterialsController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->contextSwitch()
                        ->addActionContext('showhandlyspecs', 'json')
                        ->initContext();
    }

    public function indexAction()
    {
        // action body
    }

    public function showhandlyspecsAction()
    {
        $this->view->headLink()->appendStylesheet('/stylesheets/handle_specs.css');
        $this->view->headLink()->appendStylesheet('/frameworks/fancybox/jquery.fancybox-1.3.4.css');
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/jquery/js/jquery-1.7.2.min.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/fancybox/jquery.fancybox-1.3.4.pack.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/fancybox/jquery.easing-1.3.pack.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/fancybox/jquery.mousewheel-3.0.4.pack.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/start_fansy_box.js'));
        
        $hanlySpecsService = Application_Model_HandleSpecificationsService::getInstance();
        $filter = $this->getRequest()->getParam('filter');
        $hanlySpecsPaginator = $hanlySpecsService->getPaginatorRows($filter);
        $pageId = (int) $this->getRequest()->getParam('page');
        if (isset($pageId))
        {
            $hanlySpecsPaginator->setCurrentPageNumber($pageId);
        }
        $this->view->paginator = $hanlySpecsPaginator;
        $this->view->hanlySpecs = $hanlySpecsPaginator->getCurrentItems();
        $reg = Zend_Registry::getInstance();
        $this->view->role = $reg->currentUser->get('role');
    }

    public function addhandlyspecAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'add_handly_spec')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        
        $formAddHandlySpec = new Application_Form_HandlySpec();
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formAddHandlySpec->isValid($formData))
            {
                $formAddHandlySpec->processUploadedFiles();
                $values = $formAddHandlySpec->getValues();
                $savePath = Application_Model_PathsService::getInstance()->get("handly_spec");
                $handly = new Application_Model_HandleSpecification();
                if ($savePath != null && isset($values['photos'])){
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    $handly->set('file_name', $values['photos']);
                }
                
                $handly->set('name', $formData['name']);
                $handly->set('base_width', $formData['base_width']);
                if (isset($formData['handle_width']) && $formData['handle_width'] != "")
                    $handly->set('handle_width', $formData['handle_width']);
                $handly->set('capture', $formData['capture']);
                
                $handlysService = Application_Model_HandleSpecificationsService::getInstance();
                $handlysService->save($handly);
                $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'materials',
                                                'action' => 'showhandlyspecs')));
                
                return;
            }
        }
        $this->view->formAddHandlySpec = $formAddHandlySpec;
    }

    public function delhandlyspecAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'remove_handly_specs')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $delId = (int) $this->getRequest()->getParam('id');
        if (isset($delId))
        {
            $handleSpecificationsService = Application_Model_HandleSpecificationsService::getInstance();
            $handleSpec = $handleSpecificationsService->find($delId);
            $handleSpec = $handleSpec -> current() -> toArray();
            if ($handleSpec != null){
                $file_name = $handleSpec['file_name'];
                if ($file_name != null){
                    $delPath = Application_Model_PathsService::getInstance()->get("handly_spec");
                    if (file_exists($delPath . $file_name))
                    unlink($delPath . $file_name);
                    $delPath = Application_Model_PathsService::getInstance()->get("handly_spec", true);
                    if (file_exists($delPath . $file_name))
                    unlink($delPath . $file_name);
                }
                $handleSpecificationsService-> delById($handleSpec['id']);
            }
        }
        $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showhandlyspecs')));
        
    }

    public function edithandlyspecAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'edit_news')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        $this->_helper->layout->setLayout('layout_work');
        
        $handleSpecsService = Application_Model_HandleSpecificationsService::getInstance();
        $formEditHandlySpec = new Application_Form_HandlySpec();
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formEditHandlySpec->isValid($formData))
            {
                $formEditHandlySpec->processUploadedFiles();
                $values = $formEditHandlySpec->getValues();
                $handly = $handleSpecsService->findById($values['id']);
                $newsPath = Application_Model_PathsService::getInstance()->get("handly_spec");
                if ($newsPath != null && isset($values['photos'])){
                    $newsPathIcons = Application_Model_PathsService::getInstance()->get("handly_spec", true);
                    if (file_exists($newsPathIcons . $handly->get('file_name')))
                        unlink($newsPathIcons . $handly->get('file_name'));
                    if (file_exists($newsPath . $handly->get('file_name')))
                        unlink($newsPath . $handly->get('file_name'));
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $newsPath . $values['photos']
                    );
                    $handly->set('file_name', $values['photos']);
                }
                
                $handly->set('name', $formData['name']);
                $handly->set('base_width', $formData['base_width']);
                if (isset($formData['handle_width']) && $formData['handle_width'] != "")
                    $handly->set('handle_width', $formData['handle_width']);
                $handly->set('capture', $formData['capture']);
                $handly->set('id', $formData['id']);
                
                $handlysService = Application_Model_HandleSpecificationsService::getInstance();
                $handlysService->save($handly);
                $this->_redirect(   $this
                                    ->view
                                    ->url(array('controller' => 'materials',
                                                'action' => 'showhandlyspecs')));
                
                return;
            }
        } else {
            $findId = $this->getRequest()->getParam('id');
            if (isset($findId))
            {
                $res = $handleSpecsService->findById($findId);
                if ($res != null)
                {
                    $formEditHandlySpec->populate($handleSpecsService->mapToArray($res));
                } else {
                     $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'materials',
                                            'action' =>'showhandlyspecs')));
                }
            } else {
                 $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'materials',
                                            'action' =>'showhandlyspecs')));
            }
        }
        
        $this->view->formEditHandlySpec = $formEditHandlySpec;
    }

    public function showsystempricesAction()
    {
        $this->view->headLink()->appendStylesheet('/stylesheets/systems.css');
        
        $priceSystemsService = Application_Model_PriceSystemsService::getInstance();
        $priceSystemsPaginator = $priceSystemsService->getPaginatorRows();
        $pageId = $this->getRequest()->getParam('page');
        if (isset($pageId))
        {
            $priceSystemsPaginator->setCurrentPageNumber($pageId);
        }
        $this->view->paginator = $priceSystemsPaginator;
        $this->view->priceSystems = $priceSystemsPaginator->getCurrentItems();
        $reg = Zend_Registry::getInstance();
        $this->view->role = $reg->currentUser->get('role');
    }

    public function addsystemAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'add_system')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        
        $this->view->headLink()->appendStylesheet('/frameworks/colorpicker/css/colorpicker.css');
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/jquery/js/jquery-1.7.2.min.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/colorpicker/js/colorpicker.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/start_color_picker.js'));
        
        $formAddSystem = new Application_Form_System();
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formAddSystem->isValid($formData))
            {
                $formAddSystem->processUploadedFiles();
                $values = $formAddSystem->getValues();
                
                $savePath = Application_Model_PathsService::getInstance()->get("system_textures");
                $priceSystem = new Application_Model_PriceSystem();
                
                if ($savePath != null && isset($values['photos'])){
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    $priceSystem->set('file_name', $values['photos']);
                }
                
                $priceSystem->set('name', $values['name']);
                $priceSystem->set('system_color', $values['system_color']);
                
                $priceSystem->set('price_cornice', $values['price_cornice']);
                $priceSystem->set('price_threshold', $values['price_threshold']);
                $priceSystem->set('price_top_bar', $values['price_top_bar']);
                $priceSystem->set('price_bottom_bar', $values['price_bottom_bar']);
                $priceSystem->set('price_separator', $values['price_separator']);
                
                $priceSystem->set('cost_cornice', $values['cost_cornice']);
                $priceSystem->set('cost_threshold', $values['cost_threshold']);
                $priceSystem->set('cost_top_bar', $values['cost_top_bar']);
                $priceSystem->set('cost_bottom_bar', $values['cost_bottom_bar']);
                $priceSystem->set('cost_separator', $values['cost_separator']);
                $priceSystemService = Application_Model_PriceSystemsService::getInstance();
                
                $priceSystemService->save($priceSystem);
                
                $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showsystemprices')));
                
                return;
            }
        }
        $this->view->formAddSystem = $formAddSystem;
    }

    public function editsystemAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'add_system')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        
        $this->view->headLink()->appendStylesheet('/frameworks/colorpicker/css/colorpicker.css');
        
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/jquery/js/jquery-1.7.2.min.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/colorpicker/js/colorpicker.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/start_color_picker.js'));
        
        $formAddSystem = new Application_Form_System();
        $priceSystemService = Application_Model_PriceSystemsService::getInstance();
        
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formAddSystem->isValid($formData))
            {
                $formAddSystem->processUploadedFiles();
                $values = $formAddSystem->getValues();
                
                $priceSystem = $priceSystemService-> findById($values['id']);
                $systemTexturesPath = Application_Model_PathsService::getInstance()->get("system_textures");
                
                if ($systemTexturesPath != null && isset($values['photos'])){
                    $systemTexturesPathIcons = Application_Model_PathsService::getInstance()->get("system_textures", true);
                    if (file_exists($systemTexturesPathIcons . $priceSystem->get('file_name')))
                        unlink($systemTexturesPathIcons . $priceSystem->get('file_name'));
                    if (file_exists($systemTexturesPath . $priceSystem->get('file_name')))
                        unlink($systemTexturesPath . $priceSystem->get('file_name'));
                    
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $systemTexturesPath . $values['photos']
                    );
                    $priceSystem->set('file_name', $values['photos']);
                }
                if ($savePath != null && isset($values['photos'])){
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    $priceSystem->set('file_name', $values['photos']);
                }
                
                $priceSystem->set('name', $values['name']);
                $priceSystem->set('system_color', $values['system_color']);
                
                $priceSystem->set('price_cornice', $values['price_cornice']);
                $priceSystem->set('price_threshold', $values['price_threshold']);
                $priceSystem->set('price_top_bar', $values['price_top_bar']);
                $priceSystem->set('price_bottom_bar', $values['price_bottom_bar']);
                $priceSystem->set('price_separator', $values['price_separator']);
                
                $priceSystem->set('cost_cornice', $values['cost_cornice']);
                $priceSystem->set('cost_threshold', $values['cost_threshold']);
                $priceSystem->set('cost_top_bar', $values['cost_top_bar']);
                $priceSystem->set('cost_bottom_bar', $values['cost_bottom_bar']);
                $priceSystem->set('cost_separator', $values['cost_separator']);
                $priceSystemService = Application_Model_PriceSystemsService::getInstance();
                
                $priceSystemService->save($priceSystem);
                
                $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showsystemprices')));
                
                return;
            }
        } else {
            $findId = (int) $this->getRequest()->getParam('id');
            if (isset($findId))
            {
                $priceSystem = $priceSystemService->findById($findId);
                if ($priceSystem != null)
                {   $res = $priceSystemService->mapToArray($priceSystem);
                    $formAddSystem->populate($res);
                } else {
                     $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'materials',
                                            'action' =>'showsystemprices')));
                }
            } else {
                 $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'materials',
                                            'action' =>'showsystemprices')));
            }
        }
        $this->view->formAddSystem = $formAddSystem;
    }

    public function delsystemAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'remove_system')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        $delId = (int)($this->getRequest()->getParam('id'));
        if (isset($delId))
        {
            $priceSystemService = Application_Model_PriceSystemsService::getInstance();
            $priceSystem = $priceSystemService->find($delId);
            $priceSystem = $priceSystem -> current() -> toArray();
            if ($priceSystem != null){
                $file_name = $priceSystem['file_name'];
                if ($file_name != null){
                    $delPath = Application_Model_PathsService::getInstance()->get("system_textures");
                    if (file_exists($delPath . $file_name))
                    unlink($delPath . $file_name);
                    $delPath = Application_Model_PathsService::getInstance()->get("system_textures", true);
                    if (file_exists($delPath . $file_name))
                    unlink($delPath . $file_name);
                }
                $priceSystemService-> delById($delId);
            }
            
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showsystemprices')));
        }
    }

    public function showfillpricesAction()
    {
        $this->view->headLink()->appendStylesheet('/stylesheets/fill_prices.css');
        
        $priceSystemService = Application_Model_PriceFillsService::getInstance();
        $priceSystemPaginator = $priceSystemService->getPaginatorRows();
        $pageId = (int) $this->getRequest()->getParam('page');
        if (isset($pageId))
        {
            $priceSystemPaginator->setCurrentPageNumber($pageId);
        }
        $countPerPage = (int) $this->getRequest()->getParam('countpp');
        if (isset($countPerPage))
        {
            $priceSystemPaginator->setItemCountPerPage($countPerPage);
        }
        $this->view->paginator = $priceSystemPaginator;
        $this->view->priceFills = $priceSystemPaginator->getCurrentItems();
        $reg = Zend_Registry::getInstance();
        $this->view->role = $reg->currentUser->get('role');
    }

    public function addfillpriceAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'add_fill')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        
        $this->view->headLink()->appendStylesheet('/frameworks/colorpicker/css/colorpicker.css');
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/jquery/js/jquery-1.7.2.min.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/colorpicker/js/colorpicker.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/start_color_picker.js'));
        
        $formAddFill = new Application_Form_Fill();
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formAddFill->isValid($formData))
            {
                $formAddFill->processUploadedFiles();
                $values = $formAddFill->getValues();
                //var_dump($values); die();
                
                $savePath = Application_Model_PathsService::getInstance()->get("fill_textures");
                $priceFill = new Application_Model_PriceFill();
                
                if ($savePath != null && isset($values['photos'])){
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    $priceFill->set('file_name', $values['photos']);
                }
                
                $priceFill->set('fill_color', $values['fill_color']);
                $priceFill->set('name', $values['name']);
                $priceFill->set('price', $values['price']);
                $priceFill->set('cost', $values['cost']);
                $priceFillsService = Application_Model_PriceFillsService::getInstance();
                
                $priceFillsService->save($priceFill);
                
                $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showfillprices')));
                
                return;
            }
        }
        $this->view->formAddFill = $formAddFill;
    }

    public function editfillpriceAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'edit_fill')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $this->_helper->layout->setLayout('layout_work');
        
        $this->view->headLink()->appendStylesheet('/frameworks/colorpicker/css/colorpicker.css');
        
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/jquery/js/jquery-1.7.2.min.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/frameworks/colorpicker/js/colorpicker.js'));
        $this->view->headScript()->appendFile($this->view->baseUrl('/js/start_color_picker.js'));
        
        $formAddFill = new Application_Form_Fill();
        $priceFillsService = Application_Model_PriceFillsService::getInstance();
        
        if ($this->_request->isPost())
        {
            $formData = $this->_request->getPost();
            if ($formAddFill->isValid($formData))
            {
                $formAddFill->processUploadedFiles();
                $values = $formAddFill->getValues();
                //var_dump($values); die();
                
                $priceFillsService = Application_Model_PriceFillsService::getInstance();
                $savePath = Application_Model_PathsService::getInstance()->get("fill_textures");
                $priceFill = $priceFillsService-> findById($values['id']);
                
                if ($savePath != null && isset($values['photos'])){
                    $newsPathIcons = Application_Model_PathsService::getInstance()->get("fill_textures", true);
                    if (file_exists($newsPathIcons . $priceFill->get('file_name')))
                        unlink($newsPathIcons . $priceFill->get('file_name'));
                    if (file_exists($savePath . $priceFill->get('file_name')))
                        unlink($savePath . $priceFill->get('file_name'));
                    rename(
                        Application_Model_PathsService::getInstance()->get("temp") . $values['photos'], 
                        $savePath . $values['photos']
                    );
                    $priceFill->set('file_name', $values['photos']);
                }
                
                $priceFill->set('fill_color', $values['fill_color']);
                $priceFill->set('name', $values['name']);
                $priceFill->set('price', $values['price']);
                $priceFill->set('cost', $values['cost']);
                
                
                $priceFillsService->save($priceFill);
                
                $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showfillprices')));
                
                return;
            }
        } else {
            $findId = (int) $this->getRequest()->getParam('id');
            if (isset($findId))
            {
                $priceFill = $priceFillsService->findById($findId);
                if ($priceFill != null)
                {   $res = $priceFillsService->mapToArray($priceFill);
                    $formAddFill->populate($res);
                } else {
                     $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'materials',
                                            'action' =>'showsystemprices')));
                }
            } else {
                 $this->_redirect(   $this
                                ->view
                                ->url(array('controller' => 'materials',
                                            'action' =>'showsystemprices')));
            }
        }
        $this->view->formAddFill = $formAddFill;
    }

    public function delfillpriceAction()
    {
        $registry = Zend_Registry::getInstance();
        if (!$registry->acl->isAllowed($registry->currentUser->get('role'), 'remove_fill')){
            $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'office',
                                        'action' => 'index')));
            return;
        }
        
        $delId = (int) $this->getRequest()->getParam('id');
        if (isset($delId))
        {
            $priceFillsService = Application_Model_PriceFillsService::getInstance();
            $priceFill = $priceFillsService->find($delId);
            $priceFill = $priceFill-> current()-> toArray();
            if ($priceFill != null){
                $file_name = $priceFill['file_name'];
                if ($file_name != null){
                    $delPath = Application_Model_PathsService::getInstance()->get("fill_textures");
                    if (file_exists($delPath . $file_name))
                    unlink($delPath . $file_name);
                    $delPath = Application_Model_PathsService::getInstance()->get("fill_textures", true);
                    if (file_exists($delPath . $file_name))
                    unlink($delPath . $file_name);
                }
                $priceFillsService-> delById($priceFill['id']);
            }
        }
        $this->_redirect(   $this
                            ->view
                            ->url(array('controller' => 'materials',
                                        'action' => 'showfillprices')));
    }


}







